/**
 * Mar 27, 2010
 */
package org.tomale.id.ui;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.tomale.id.User;

/**
 * @author ferd
 *
 */
public class SignInDialog extends TitleAreaDialog {

	Text _user;
	Text _pw;
	
	final int MAX_TRIES = 3;
	int _tries = 0;
	
	public SignInDialog(Shell parent){
		super(parent);
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents =  super.createContents(parent);
		setTitle("Please sign in");
		setMessage("A plugin requires that you sign in to retrieve data from a back end service.");
//		setTitleImage(Images.getImage("icons/policies.png"));
		return contents;	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite comp = (Composite) super.createDialogArea(parent);
		
		Composite content = new Composite(comp, SWT.NO_FOCUS);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		content.setLayout(layout);
		content.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,1));
		
		GridData gd;
		
		// user name
		Label lbl = new Label(content, SWT.NONE);
		lbl.setText("User");
		lbl.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false,1,1));
		
		_user = new Text(content,SWT.BORDER);
		gd = new GridData(SWT.FILL,SWT.FILL,true,false,1,1);
		_user.setLayoutData(gd);
		
		// password
		lbl = new Label(content, SWT.NONE);
		lbl.setText("Password");
		lbl.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false,1,1));
		
		_pw = new Text(content,SWT.BORDER| SWT.PASSWORD);
		gd = new GridData(SWT.FILL,SWT.FILL,true,false,1,1);
		_pw.setLayoutData(gd);
		
		return comp;	}

	@Override
	protected void okPressed() {
		toggleUI(false);
		
		String name = _user.getText();
		String pw = _pw.getText();
		
		try {
			User.getInstance().signIn(name, pw);
			SignInDialog.this.close();
		}catch(SecurityException e){
			_tries++;
			if(_tries>MAX_TRIES){
				SignInDialog.this.close();
			}
			setErrorMessage(e.getMessage());
		}
		
		toggleUI(true);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Sign in");
//		newShell.setImage(Images.getImage("icons/lock.png"));
		newShell.setSize(350, 250);
		
		Rectangle parent = getParentShell().getBounds();
		Rectangle child = newShell.getBounds();
		int x = Math.abs(parent.width - child.width)/2 + parent.x;
		int y = Math.abs(parent.height - child.width)/2 + parent.y;
		newShell.setLocation(x,y);	
	}
	
	private void toggleUI(final boolean enabled){
		_user.setEnabled(enabled);
		_pw.setEnabled(enabled);
		
		getButton(SWT.OK).setEnabled(enabled);
		getButton(SWT.CANCEL).setEnabled(enabled);
	}
}
